package com.hang.jvm.risc.v2_memory_bus;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

    public static void main(String[] args) {
        if (args.length == 0) {
            log.error("请传入bin文件");
            return;
        }
        byte[] code = FileUtil.readBytes(args[0]);
        log.info("code length:{}", code.length);

        Cpu cpu = new Cpu(code);

        while (true) {
            long inst = 0;
            // 取指令
            try {
                inst = cpu.fetch();
            } catch (Exception e) {
                log.error("取指令异常: ", e);
                break;
            }
            // 执行指令
            try {
                long nextPc = cpu.execute(inst);
                cpu.setPc(nextPc);
            } catch (Exception e) {
                log.error("执行指令异常：", e);
                break;
            }
        }

        // 打印寄存器
        cpu.dumpRegisters();
        cpu.dumpPc();

    }

}
